package restful.struts2.handler;

import java.io.*;

import org.yaml.snakeyaml.*;

public class YamlHandler implements ContentTypeHandler {

	public String fromObject(Object obj, String resultCode, Writer stream) {
		try {
			stream.write(new Yaml().dump(obj));
		}
		catch(IOException e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	public String getContentType() {
		return "application/yaml";
	}

	public String getExtension() {
		return "yaml";
	}

	public Object toObject(Reader in, Object target) {
		return new Yaml().load(in);
	}

	public Object toObject(String in, Object target) {
		return new Yaml().load(in);
	}
}
